﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SAD_SiS.Models;
using SAD_SiS.ViewModels;

namespace SAD_SiS.Controllers
{
    public class ShopHomeController : Controller
    {
        //
        // GET: /ShopHome/
        private int PageSize = 1;

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Views(string id, int page = 1, int cate = 0)
        {
            using (SADEntities dbContext = new SADEntities())
            {
                var shop = (from s in dbContext.Shops where s.LinkName.Equals(id) && s.Active == true select s).SingleOrDefault();
                if (shop == null)
                {
                    return View("Error");
                }
                ShopPagingView returnView = new ShopPagingView();
                returnView.CurrentShop = shop;

                if (cate == 0)
                {
                    var productList = (from p in dbContext.V_Product where p.Active == true && p.ShopID == shop.ShopID orderby p.CreatedDate descending select p).ToList();
                    var categoryList = (from p in dbContext.Products where p.Active == true && p.ShopID == shop.ShopID select p.CateID).ToList();
                    List<int> cateList = new List<int>();
                    foreach (var item in categoryList)
                    {
                        if (!cateList.Contains((int)item))
                        {
                            cateList.Add((int)item);
                        }
                    }
                    List<Category> cates = new List<Category>();
                    for (int i = 0; i < cateList.Count; i++)
                    {
                        int cid = cateList[i];
                        var inCate = (from c in dbContext.Categories where c.Active == true && c.CateID == cid select c).Single();
                        cates.Add(inCate);
                    }
                    returnView.CategoryList = cates;
                    returnView.SelectCate = null;
                    returnView.TotalItem = productList.Count;
                    returnView.ItemsPerPage = PageSize;
                    returnView.CurrentPage = page;
                    returnView.TotalPage = (int)Math.Ceiling((decimal)returnView.TotalItem / returnView.ItemsPerPage);
                    returnView.ProductList = productList.Skip((page - 1) * PageSize).Take(PageSize).ToList();
                    return View(returnView);
                }
                else
                {
                    var productList = (from p in dbContext.V_Product where p.Active == true && p.ShopID == shop.ShopID && p.CateID==cate orderby p.CreatedDate descending select p).ToList();
                    var categoryList = (from p in dbContext.Products where p.Active == true && p.ShopID == shop.ShopID select p.CateID).ToList();
                    List<int> cateList = new List<int>();
                    foreach (var item in categoryList)
                    {
                        if (!cateList.Contains((int)item))
                        {
                            cateList.Add((int)item);
                        }
                    }
                    List<Category> cates = new List<Category>();
                    for (int i = 0; i < cateList.Count; i++)
                    {
                        int cid = cateList[i];
                        var inCate = (from c in dbContext.Categories where c.Active == true && c.CateID == cid select c).Single();
                        cates.Add(inCate);
                    }
                    var selectCate = (from c in dbContext.Categories where c.Active == true && c.CateID == cate select c).Single();
                    returnView.CategoryList = cates;
                    returnView.SelectCate = selectCate;
                    returnView.TotalItem = productList.Count;
                    returnView.ItemsPerPage = PageSize;
                    returnView.CurrentPage = page;
                    returnView.TotalPage = (int)Math.Ceiling((decimal)returnView.TotalItem / returnView.ItemsPerPage);
                    returnView.ProductList = productList.Skip((page - 1) * PageSize).Take(PageSize).ToList();
                    return View(returnView);
                }
            }
            return View();
        }

        [HttpPost]
        public ActionResult Views(FormCollection form)
        {
            string id = form["fID"];
            int cate = Convert.ToInt16(form["fCate"]);
            int page = 1;
            using (SADEntities dbContext = new SADEntities())
            {
                var shop = (from s in dbContext.Shops where s.LinkName.Equals(id) && s.Active == true select s).SingleOrDefault();
                if (shop == null)
                {
                    return View("Error");
                }
                ShopPagingView returnView = new ShopPagingView();
                returnView.CurrentShop = shop;

                if (cate == 0)
                {
                    var productList = (from p in dbContext.V_Product where p.Active == true && p.ShopID == shop.ShopID orderby p.CreatedDate descending select p).ToList();
                    var categoryList = (from p in dbContext.Products where p.Active == true && p.ShopID == shop.ShopID select p.CateID).ToList();
                    List<int> cateList = new List<int>();
                    foreach (var item in categoryList)
                    {
                        if (!cateList.Contains((int)item))
                        {
                            cateList.Add((int)item);
                        }
                    }
                    List<Category> cates = new List<Category>();
                    for (int i = 0; i < cateList.Count; i++)
                    {
                        int cid = cateList[i];
                        var inCate = (from c in dbContext.Categories where c.Active == true && c.CateID == cid select c).Single();
                        cates.Add(inCate);
                    }
                    returnView.CategoryList = cates;
                    returnView.SelectCate = null;
                    returnView.TotalItem = productList.Count;
                    returnView.ItemsPerPage = PageSize;
                    returnView.CurrentPage = page;
                    returnView.TotalPage = (int)Math.Ceiling((decimal)returnView.TotalItem / returnView.ItemsPerPage);
                    returnView.ProductList = productList.Skip((page - 1) * PageSize).Take(PageSize).ToList();
                    return View(returnView);
                }
                else
                {
                    var productList = (from p in dbContext.V_Product where p.Active == true && p.ShopID == shop.ShopID && p.CateID == cate orderby p.CreatedDate descending select p).ToList();
                    var categoryList = (from p in dbContext.Products where p.Active == true && p.ShopID == shop.ShopID select p.CateID).ToList();
                    List<int> cateList = new List<int>();
                    foreach (var item in categoryList)
                    {
                        if (!cateList.Contains((int)item))
                        {
                            cateList.Add((int)item);
                        }
                    }
                    List<Category> cates = new List<Category>();
                    for (int i = 0; i < cateList.Count; i++)
                    {
                        int cid = cateList[i];
                        var inCate = (from c in dbContext.Categories where c.Active == true && c.CateID == cid select c).Single();
                        cates.Add(inCate);
                    }
                    var selectCate = (from c in dbContext.Categories where c.Active == true && c.CateID == cate select c).Single();
                    returnView.CategoryList = cates;
                    returnView.SelectCate = selectCate;
                    returnView.TotalItem = productList.Count;
                    returnView.ItemsPerPage = PageSize;
                    returnView.CurrentPage = page;
                    returnView.TotalPage = (int)Math.Ceiling((decimal)returnView.TotalItem / returnView.ItemsPerPage);
                    returnView.ProductList = productList.Skip((page - 1) * PageSize).Take(PageSize).ToList();
                    return View(returnView);
                }
            }
            return View();
        }
    }
}
